<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问题标题" prop="bzzxWtbt">
        <el-input
          v-model="queryParams.bzzxWtbt"
          placeholder="请输入问题标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="问题内容" prop="bzzxWtnr">-->
<!--        <el-input-->
<!--          v-model="queryParams.bzzxWtnr"-->
<!--          placeholder="请输入问题内容"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="问题分类id" prop="wtflId">-->
<!--        <el-input-->
<!--          v-model="queryParams.wtflId"-->
<!--          placeholder="请输入问题分类id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="问题分类" prop="formData.field101">
        <el-select v-model="queryParams.formData.field101" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in queryParams.field101Options" :key="index" :label="item.wtflName"
                     :value="item.wtflId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="客户端id" prop="khdId">-->
<!--        <el-input-->
<!--          v-model="queryParams.khdId"-->
<!--          placeholder="请输入客户端id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="排序号" prop="bzzxPxh">-->
<!--        <el-input-->
<!--          v-model="queryParams.bzzxPxh"-->
<!--          placeholder="请输入排序号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="bzzxCreatetime">-->
<!--        <el-input-->
<!--          v-model="queryParams.bzzxCreatetime"-->
<!--          placeholder="请输入创建时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="状态id" prop="ztId">-->
<!--        <el-input-->
<!--          v-model="queryParams.ztId"-->
<!--          placeholder="请输入状态id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="客户端" prop="formData1.field101">
        <el-select v-model="queryParams.formData1.field101" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in queryParams.field101Options1" :key="index" :label="item.khdName"
                     :value="item.khdId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="formData2.field101">
        <el-select v-model="queryParams.formData2.field101" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in queryParams.field101Options2" :key="index" :label="item.ztName"
                     :value="item.ztId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <div class="block">
        <span class="demonstration">创建时间</span>
        <el-date-picker
          v-model="value"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </div>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bangzhuzhongxin:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bangzhuzhongxin:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bangzhuzhongxin:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:bangzhuzhongxin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bangzhuzhongxinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="bzzxId" />
      <el-table-column label="问题标题" align="center" prop="bzzxWtbt" />
<!--      <el-table-column label="问题内容" align="center" prop="bzzxWtnr" />-->
      <el-table-column label="问题分类" align="center" prop="wentifenlei.wtflName" />
      <el-table-column label="客户端" align="center" prop="kehuduan.khdName" />
      <el-table-column label="排序号" align="center" prop="bzzxPxh" />
      <el-table-column label="创建时间" align="center" prop="bzzxCjsj" />
      <el-table-column label="状态" align="center" prop="zhuangtai.ztName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bangzhuzhongxin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-warning"
            @click="change(scope.row)"
            v-hasPermi="['system:bangzhuzhongxin:edit']"
          >启用/禁用问题</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bangzhuzhongxin:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改帮助中心对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题标题" prop="bzzxWtbt">
          <el-input v-model="form.bzzxWtbt" placeholder="请输入问题标题" />
        </el-form-item>
        <el-form-item label="问题内容" prop="bzzxWtnr">
          <el-input v-model="form.bzzxWtnr" placeholder="请输入问题内容" />
        </el-form-item>
        <el-form-item label="问题分类">
          <el-select v-model="form.wtflId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in queryParams.field101Options" :key="index" :label="item.wtflName"
                       :value="item.wtflId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户端">
          <el-select v-model="form.khdId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in queryParams.field101Options1" :key="index" :label="item.khdName"
                       :value="item.khdId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序号" prop="bzzxPxh">
          <el-input v-model="form.bzzxPxh" placeholder="请输入排序号" />
        </el-form-item>
<!--        <el-form-item label="创建时间" prop="bzzxCreatetime">-->
<!--          <el-input v-model="form.bzzxCreatetime" placeholder="请输入创建时间" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="状态id" prop="ztId">-->
<!--          <el-input v-model="form.ztId" placeholder="请输入状态id" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBangzhuzhongxin, getBangzhuzhongxin, delBangzhuzhongxin, addBangzhuzhongxin, updateBangzhuzhongxin,listWentifenlei ,listKehuduan ,listZhuangtai } from "@/api/system/bangzhuzhongxin";

export default {
  name: "Bangzhuzhongxin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 帮助中心表格数据
      bangzhuzhongxinList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //创建时间范围
      value:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bzzxWtbt: null,
        bzzxWtnr: null,
        wtflId: null,
        khdId: null,
        bzzxPxh: null,
        bzzxCjsj: null,
        ztId: null,

        //问题分类的查询参数
        formData: {
          field101: undefined,
        },
        rules: {
          field101: [{
            required: true,
            message: '请选择下拉选择',
            trigger: 'change'
          }],
        },
        field101Options: [],

        //客户端的查询参数
        formData1: {
          field101: undefined,
        },
        rules1: {
          field101: [{
            required: true,
            message: '请选择下拉选择',
            trigger: 'change'
          }],
        },
        field101Options1: [],

        //状态的查询参数
        formData2: {
          field101: undefined,
        },
        rules2: {
          field101: [{
            required: true,
            message: '请选择下拉选择',
            trigger: 'change'
          }],
        },
        field101Options2: [],
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      zt_zc: -1,
      zt_yjy: -1,

    };
  },
  created() {
    this.getList();
    this.getWtflList();
    this.getKhdList();
    this.getZtList();
  },
  methods: {
    /** 查询帮助中心列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};

      if (null != this.value && '' != this.value) {
        var a = this.getGoodTime(this.value[0])
        var b = this.getGoodTime(this.value[1])
        this.queryParams.params["beginBzzxCjsj"] = a;
        this.queryParams.params["endBzzxCjsj"] = b;
      }
      this.queryParams.wtflId = this.queryParams.formData.field101;
      this.queryParams.khdId = this.queryParams.formData1.field101;
      this.queryParams.ztId = this.queryParams.formData2.field101;
      listBangzhuzhongxin(this.queryParams).then(response => {
        this.bangzhuzhongxinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询问题分类 */
    getWtflList(){
      listWentifenlei().then(response => {
        this.queryParams.field101Options = response.rows;
      });
    },
    /** 查询客户端 */
    getKhdList(){
      listKehuduan().then(response => {
        this.queryParams.field101Options1 = response.rows;
      });
    },
    /** 查询状态 */
    getZtList(){
      listZhuangtai().then(response => {
        this.queryParams.field101Options2 = response.rows.slice(3,5);
        for (let i = 0; i < response.rows.length; i++) {
          if (response.rows[i].ztName == "正常"){
            this.zt_zc = i + 1;
          }else if (response.rows[i].ztName == "已禁用"){
            this.zt_yjy = i + 1;
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bzzxId: null,
        bzzxWtbt: null,
        bzzxWtnr: null,
        wtflId: null,
        khdId: null,
        bzzxPxh: null,
        bzzxCjsj: null,
        ztId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.value = '';
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bzzxId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加帮助中心";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bzzxId = row.bzzxId || this.ids
      getBangzhuzhongxin(bzzxId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改帮助中心";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.bzzxCjsj = this.getGoodTime(new Date());
        this.form.ztId = this.zt_zc;
        console.log(this.zt_zc)
        console.log(this.form.ztId)
        if (valid) {
          if (this.form.bzzxId != null) {
            updateBangzhuzhongxin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBangzhuzhongxin(this.form).then(response => {
              console.log(this.form)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bzzxIds = row.bzzxId || this.ids;
      this.$modal.confirm('是否确认删除帮助中心编号为"' + bzzxIds + '"的数据项？').then(function() {
        return delBangzhuzhongxin(bzzxIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 改变按钮操作 */
    change(row){
      this.reset();
      const bzzxId = row.bzzxId || this.ids;
      getBangzhuzhongxin(bzzxId).then(response => {
        this.form = response.data;
        if(this.form.ztId == this.zt_zc){
          this.form.ztId = this.zt_yjy
        }else if(this.form.ztId == this.zt_yjy){
          this.form.ztId = this.zt_zc
        }
        updateBangzhuzhongxin(this.form).then(response => {
          this.getList();
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bangzhuzhongxin/export', {
        ...this.queryParams
      }, `bangzhuzhongxin_${new Date().getTime()}.xlsx`)
    },
    //时间格式转化
    getGoodTime(time) {
      // 尝试将输入字符串解析为Date对象
      let date = new Date(time);
      // 检查Date对象是否有效（即不是Invalid Date）
      if (isNaN(date.getTime())) {
        throw new Error('Invalid date string');
      }
      // 提取年、月、日、时、分、秒
      // 月份从0开始，所以要加1；日期、小时、分钟、秒直接使用
      // 使用padStart来确保月份、日期、小时、分钟、秒始终是两位数
      const year = time.getFullYear();
      const month = String(time.getMonth() + 1).padStart(2, '0');
      const day = String(time.getDate()).padStart(2, '0');
      const hours = String(time.getHours()).padStart(2, '0');
      const minutes = String(time.getMinutes()).padStart(2, '0');
      const seconds = String(time.getSeconds()).padStart(2, '0');

      // 拼接成所需的格式
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
  }
};
</script>
